﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml;

using Elegant.Ui;
using PdfSharp.Drawing;

namespace iPdf
{
    class ImageWatermarkPage : WatermarkPage
    {
        #region Properties

        private XImage WatermarkImage
        {
            get
            {
                Image image = this.WatermarkImages.HoveredImage != null ?
                    this.WatermarkImages.HoveredImage :
                    this.WatermarkImages.SelectedImage;
                return XImage.FromGdiPlusImage( ImageProcessor.Transparentify(image, this.WatermarkLayout.Opacity) );
            }
        }

        private bool ImageSelected
        {
            get
            {
                return this.WatermarkImages.Enabled && this.WatermarkImages.SelectedItem != null;
            }
        }

        public override bool ReadyToAct
        {
            get
            {
                return base.ReadyToAct && this.ImageSelected;
            }
        }

        protected override WatermarkOption PageOption
        {
            get
            {
                WatermarkOption option = new WatermarkOption();
                option.Element = this.WatermarkImage;
                option.Layout = this.WatermarkLayout;

                return option;
            }
        }

        public override string IntroInfo
        {
            get
            {
                return global::iPdf.Properties.Resources.ImageWatermarkPageIntroInfo;
            }
        }

        #endregion

        #region Ctor

        public ImageWatermarkPage()
            : base()
        {
            actor = new ImageWatermarker();
        }

        protected override void InitializeComponent()
        {
            base.InitializeComponent();

            this.ImageGroup = new Elegant.Ui.RibbonGroup();
            this.WatermarkImages = new iPdf.WatermarkImagesGallery();
            this.LayoutGallery = new iPdf.ImageLayoutGallery();

            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();

            ((System.ComponentModel.ISupportInitialize)(this.ImageGroup)).BeginInit();
            this.ImageGroup.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.WatermarkImages)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.WatermarkImages.Popup)).BeginInit();
            this.LayoutGroup.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.LayoutGallery)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.LayoutGallery.Popup)).BeginInit();

            this.SuspendLayout();

            this.Controls.Add(this.ImageGroup);
            this.Controls.Add(this.LayoutGroup);
            this.Controls.Add(this.PreviewGroup);
            // 
            // ImageGroup
            // 
            this.ImageGroup.Controls.Add(this.WatermarkImages);
            this.ImageGroup.DialogLauncherButtonVisible = false;
            this.ImageGroup.Id = "c6e5f708-03d2-4eca-bf1c-50a804381835";
            this.ImageGroup.Location = new System.Drawing.Point(4, 3);
            this.ImageGroup.Name = "ImageGroup";
            this.ImageGroup.Size = new System.Drawing.Size(49, 0);
            this.ImageGroup.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Image16)});
            this.ImageGroup.TabIndex = 3;
            this.ImageGroup.Text = "Image";
            // 
            // WatermarkImages
            // 
            this.WatermarkImages.Id = "2b34928d-ecef-4b72-84dc-f9729c89cce4";
            this.WatermarkImages.ItemContentPadding = new System.Windows.Forms.Padding(6);
            this.WatermarkImages.ItemHeight = 62;
            this.WatermarkImages.ItemWidth = 80;
            this.WatermarkImages.Location = new System.Drawing.Point(14, 5);
            this.WatermarkImages.MaximumItemsInRowCount = 3;
            this.WatermarkImages.MinimumItemsInRowCount = 1;
            this.WatermarkImages.Name = "WatermarkImages";
            this.WatermarkImages.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.ImagesGallery16)});
            // 
            // 
            // 
            this.WatermarkImages.Popup.PlacementMode = Elegant.Ui.PopupPlacementMode.Bottom;
            this.WatermarkImages.Popup.Size = new System.Drawing.Size(100, 100);
            this.WatermarkImages.PopupMinimumItemsInRowCount = 3;
            this.WatermarkImages.Size = new System.Drawing.Size(19, 0);
            this.WatermarkImages.TabIndex = 0;
            this.WatermarkImages.Text = "Watermark Images Gallery";

            this.LayoutGroup.Controls.Add(this.LayoutGallery);
            this.LayoutGroup.Controls.SetChildIndex(this.LayoutGallery, 0);
            // 
            // LayoutGallery
            // 
            this.LayoutGallery.Id = "120d46fb-7641-4a83-ad3c-135729461f01";
            this.LayoutGallery.ItemContentBackgroundColor = System.Drawing.Color.LightSkyBlue;
            this.LayoutGallery.ItemContentPadding = new System.Windows.Forms.Padding(6);
            this.LayoutGallery.ItemHeight = 62;
            this.LayoutGallery.ItemWidth = 78;
            this.LayoutGallery.Location = new System.Drawing.Point(12, 5);
            this.LayoutGallery.MaximumItemsInRowCount = 2;
            this.LayoutGallery.MinimumItemsInRowCount = 1;
            this.LayoutGallery.Name = "LayoutGallery";
            // 
            // 
            // 
            this.LayoutGallery.Popup.PlacementMode = Elegant.Ui.PopupPlacementMode.Bottom;
            this.LayoutGallery.Popup.Size = new System.Drawing.Size(100, 100);
            this.LayoutGallery.Size = new System.Drawing.Size(19, 0);
            this.LayoutGallery.TabIndex = 15;
            this.LayoutGallery.Text = "Image Layout Gallery";



            ((System.ComponentModel.ISupportInitialize)(this.ImageGroup)).EndInit();
            this.ImageGroup.ResumeLayout(false);
            this.ImageGroup.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.WatermarkImages.Popup)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.WatermarkImages)).EndInit();
            this.LayoutGroup.ResumeLayout(false);
            this.LayoutGroup.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.LayoutGallery.Popup)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.LayoutGallery)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        #endregion

        #region Methods

        protected override void RenderPage(XGraphics gfx)
        {
            SharpHelper.AddImageToXGraphics(gfx, this.WatermarkImage, this.WatermarkLayout);
        }

        protected override void InstallEventHandlers()
        {
            base.InstallEventHandlers();

            this.WatermarkImages.SelectedItemChanged += new EventHandler<Elegant.Ui.GallerySelectedItemChangedEventArgs>
                                    (WatermarkImagesGallery_SelectedItemChanged);
            this.WatermarkImages.HoveredItemChanged += new EventHandler<GalleryHoveredItemChangedEventArgs>
                (Gallery_HoveredItemChanged);
        }

        private void WatermarkImagesGallery_SelectedItemChanged(object sender, GallerySelectedItemChangedEventArgs e)
        {
            UpdatePageOnlyControls();
            UpdatePreviewer();
        }


        public override void UpdatePageOnlyControls()
        {
            bool enabled = !Global.MainWindow.Busy && Global.Grid.HasItems;
            Helper.EnableControl(this.WatermarkImages, enabled);

            layoutGroupEnabled = enabled && ImageSelected;
            base.UpdatePageOnlyControls();
        }

        public override void Load()
        {
            base.Load();

            WatermarkImages.Load();
        }

        public override void ImportSession(XmlElement sessionNode)
        {
            base.ImportSession(sessionNode);

            XmlElement markNode = (XmlElement)sessionNode.GetElementsByTagName(markTag)[0];
            string imageFilePath = markNode.GetAttribute(imageAttr);
            this.WatermarkImages.SetSelectedImage(imageFilePath);
        }

        public override void ExportSession(System.Xml.XmlElement sessionNode)
        {
            base.ExportSession(sessionNode);

            XmlElement markNode = sessionNode.OwnerDocument.CreateElement(markTag);
            GalleryItem imageGalleryItem = (GalleryItem)this.WatermarkImages.SelectedItem;
            string imageFilePath = (string) imageGalleryItem.Data;
            markNode.SetAttribute(imageAttr, imageFilePath);

            sessionNode.AppendChild(markNode);
        }

        #endregion

        #region Fields
        private Elegant.Ui.RibbonGroup ImageGroup;
        private WatermarkImagesGallery WatermarkImages;
        private string imageAttr = "Image";
        #endregion
    }
}
